package com.mh1004.main;

import java.util.*;

import android.os.*;
import android.support.v4.app.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends FragmentActivity implements OnClickListener {

	/** 탭 메뉴 종류 */
	public static final int FRAGMENT_MAIN = 0; // 메인
	public static final int FRAGMENT_RESULT = 1; // 실적
	public static final int FRAGMENT_POINT_OUT = 2; // 출금
	public static final int FRAGMENT_FRIEND = 3; // 추천
	public static final int FRAGMENT_ORDER = 4; // 주문내역
	
	/** 탭메뉴 버튼 리스트 */
	private List<Button> buttons;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button main = (Button) findViewById(R.id.menu1_main);
		Button result = (Button) findViewById(R.id.menu2_result);
		Button pointOut = (Button) findViewById(R.id.menu3_point_out);
		Button friend = (Button) findViewById(R.id.menu4_friend);
		Button order = (Button) findViewById(R.id.menu5_order);

		main.setOnClickListener(this);
		result.setOnClickListener(this);
		pointOut.setOnClickListener(this);
		friend.setOnClickListener(this);
		order.setOnClickListener(this);
		
		buttons = new ArrayList<Button>();
		buttons.add(main);
		buttons.add(result);
		buttons.add(pointOut);
		buttons.add(friend);
		buttons.add(order);

		replaceFragment(FRAGMENT_MAIN);
	}

	/**
	 * 입력된 인덱스로 프레그먼트 교체
	 * 
	 * @param index
	 */
	public void replaceFragment(int index) {
		Fragment newFragment = createFragment(index);
		final FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.fragment, newFragment);
		transaction.commit();
	}

	/**
	 * 입력된 인덱스 탭메뉴 선택버튼 교체
	 * @param index
	 */
	public void replaceButtonSelected(int index) {
		for (int i = 0; i < buttons.size(); i++) {
			if (i == index) {
				buttons.get(i).setSelected(true);
			} else {
				buttons.get(i).setSelected(false);
			}
		}
	}

	/**
	 * 입력된 인덱스 번호로 프레그먼트 클래스 생성
	 * 
	 * @param index
	 * @return
	 */
	public Fragment createFragment(int index) {
		switch (index) {
		case FRAGMENT_MAIN:
			return new MainFragment();
		case FRAGMENT_RESULT:
			return new ResultFragment();
		case FRAGMENT_POINT_OUT:
			return new PointOutFragment();
		case FRAGMENT_FRIEND:
			return new FriendFragment();
		case FRAGMENT_ORDER:
			return new OrderFragment();
		}
		return null;
	}

	@Override
	public void onClick(View v) {
		int index = 0;
		switch (v.getId()) {
		case R.id.menu1_main:
			index = FRAGMENT_MAIN;
			break;
		case R.id.menu2_result:
			index = FRAGMENT_RESULT;
			break;
		case R.id.menu3_point_out:
			index = FRAGMENT_POINT_OUT;
			break;
		case R.id.menu4_friend:
			index = FRAGMENT_FRIEND;
			break;
		case R.id.menu5_order:
			index = FRAGMENT_ORDER;
			break;
		}
		replaceFragment(index);
		replaceButtonSelected(index);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}